local skynet = require "skynet"
local tabletool = require "tabletool"
local base = require "base"
local slottool = require "slottool"
local redisdao = require "redisdao"
local configdao = require "configdao"
local filelog = require "filelog"
local json = require "cjson"
json.encode_sparse_array(true,1,1)

require "pubenum"
require "sssenum"

local SystemLoseLimit_Base = 0
local SystemLoseLimit_Factor = 0

local call_banker_weights_niu = {
    [1] = 50,
    [2] = 0,
    [3] = 0,
    [4] = 0,
    [5] = 50,
}

local call_banker_weights_noniu = {
    [1] = 70,
    [2] = 0,
    [3] = 0,
    [4] = 0,
    [5] = 30,
}

local xiazhu_weights_niu = {
    [1] = 50,
    [2] = 0,
    [3] = 50,
}

local xiazhu_weights_noniu = {
    [1] = 50,
    [2] = 0,
    [3] = 50,
}

local robot_leave_table_weight = {
    [1] = {
        [1] = 100,
        [2] = 0,
    },
    [2] = {
        [1] = 70,
        [2] = 30,
    },
    [3] = {
        [1] = 60,
        [2] = 40,
    },
    [4] = {
        [1] = 40,
        [2] = 60,
    },
    [5] = {
        [1] = 20,
        [2] = 80,
    },
    [6] = {
        [1] = 0,
        [2] = 100,
    },
}

local function getsvalueopenconf()
	local dnnodeconf = nil
	local dnsvalueopenconf = nil
	local result, data = redisdao.query_data(".gmmqredis", "get", "dnnode_conf")
	if result and data then
		dnnodeconf = json.decode(data)
		dnsvalueopenconf = tabletool.deepcopy(dnnodeconf.svalue_open_conf)
	end
	if not dnsvalueopenconf then
		return nil
	else
		return dnsvalueopenconf
	end
end
local CMD = {}




function CMD.gettablewininfo(gamename)
	local sys_money = slottool.get_static_inventory_from_redis(gamename)
    local get_index = 1
    local svalueconf = getsvalueopenconf()
    for index, open_conf in ipairs(svalueconf) do
        if open_conf.s_down == nil and open_conf.s_up then
            if sys_money < open_conf.s_up then
                local temp_random = base.get_random(1,100) / 100
                temp_random = temp_random - open_conf.win 
                if temp_random <= 0 then
                    get_index = 2
                    break
                else
                    temp_random = temp_random - open_conf.normal
                    if temp_random <= 0 then
                        get_index = 1
                        break
                    else
                        temp_random = temp_random - open_conf.lose
                        if temp_random <= 0 then get_index = 3 end
                    end
                end
            end
        elseif open_conf.s_down and open_conf.s_up == nil then
            if sys_money >= open_conf.s_down then
                local temp_random = base.get_random(1,100) / 100
                temp_random = temp_random - open_conf.win 
                if temp_random <= 0 then
                    get_index = 2
                    break
                else
                    temp_random = temp_random - open_conf.normal
                    if temp_random <= 0 then
                        get_index = 1
                        break
                    else
                        temp_random = temp_random - open_conf.lose
                        if temp_random <= 0 then get_index = 3 end
                    end
                end
            end
        else
            if sys_money >= open_conf.s_down and sys_money < open_conf.s_up then
                local temp_random = base.get_random(1,100) / 100
                temp_random = temp_random - open_conf.win 
                if temp_random <= 0 then
                    get_index = 2
                    break
                else
                    temp_random = temp_random - open_conf.normal
                    if temp_random <= 0 then
                        get_index = 1
                        break
                    else
                        temp_random = temp_random - open_conf.lose
                        if temp_random <= 0 then get_index = 3 end
                    end
                end
            end
        end
    end
    return get_index
end

--
--设置系统输钱上限
--
function CMD.set_system_lose_limit(loselimit_base, loselimit_factor)
    loselimit_base = loselimit_base or 0
    loselimit_factor = loselimit_factor or 0.02
    if loselimit_base < 0 or loselimit_factor < 0 then
        return false
    end
    SystemLoseLimit_Base = loselimit_base
    SystemLoseLimit_Factor = loselimit_factor
    return true
end

function CMD.get_system_inventory(gamename)
	return slottool.get_static_inventory_from_redis(gamename)
end


--
--更新库存值
--
function CMD.update_system_inventory(gamename, val, comment)
    return slottool.update_system_inventory(gamename, val, comment)
end

--
--更新收益值
--
function CMD.update_system_profit(gamename, bet_value, comment)
    local tax_rate = slottool.get_rate_from_redis(gamename)
    local profit_val  = math.floor(bet_value * tax_rate * 100) / 100

    --更新收益表
    local result = slottool.update_system_profit(gamename, profit_val, comment)
    if result then
        return profit_val
    end
    return 0
end

function CMD.update_system_water_profit(gamename, update_value)
    local key = (gamename or "") .. "_system_water_profit"
    local _,sys_water_profit = redisdao.query_data(".gmmqredis", "get", key)
    if not sys_water_profit then
        sys_water_profit = 0
    else
        sys_water_profit = tonumber(sys_water_profit) or 0
    end
    local beginvalue = sys_water_profit
    local endvalue = sys_water_profit + update_value
    if beginvalue ~= endvalue then
        redisdao.query_data(".gmmqredis", "set", key, endvalue)
    end
end

--
--处理结算结果
--@total_change 总变化值
--@total_water  总流水值
--@total_bet    玩家总下注
--
function CMD.handle_balance_result(gamename, total_change, total_water, total_bet, comment)
    ---filelog.sys_error("-----handle_balance_result----", gamename, total_change, total_water, total_bet)
    local tax_rate = slottool.get_tax_rate_from_redis(gamename)
    -- if tax_rate < 0.3 then
    --     tax_rate = 0.3
    -- end
    --local profit_val = math.floor(total_bet * tax_rate * 100) / 100
    --local profit_val = math.floor(total_bet * tax_rate)
    local profit_val = math.floor(math.abs(total_change) * tax_rate)
    local inventory_val = -total_change
    --CMD.update_system_water_profit(gamename, inventory_val)
    ---filelog.sys_error("-------tax_rate----", tax_rate, profit_val, inventory_val)
    --更新收益值
    local result1 = slottool.update_system_profit(gamename, profit_val, comment)
    if result1 then
        inventory_val = inventory_val - profit_val
    end

    --更新库存值，扣除抽水
    local service_fee_rate = slottool.get_service_fee_rate_from_redis(gamename)
    --local service_fee_val = math.floor(total_water * service_fee_rate * 100) / 100
    local service_fee_val = math.floor(total_water * service_fee_rate)
    inventory_val = inventory_val - service_fee_val
    ---filelog.sys_error("------service_fee_rate------", service_fee_rate, service_fee_val, inventory_val)
    return slottool.update_system_inventory(gamename, inventory_val, comment)
end

--
--获取抽水率
--
function CMD.get_service_fee_rate(gamename)
    return slottool.get_service_fee_rate_from_redis(gamename)
end

function CMD.get_system_inventory(gamename)
	return slottool.get_static_inventory_from_redis(gamename)
end

function CMD.get_callbanker_times(gamename, is_has_niu)
    local is_has_niu = is_has_niu or false
    local callbanker_times = 0
    if is_has_niu == true then
        local total_w = slottool.calculate_total_weight(call_banker_weights_niu)
        local w_times = slottool.get_random_result_by_weightlist(call_banker_weights_niu, total_w)
        callbanker_times = w_times - 1
    else
        local total_w = slottool.calculate_total_weight(call_banker_weights_noniu)
        local w_times = slottool.get_random_result_by_weightlist(call_banker_weights_noniu, total_w)
        callbanker_times = w_times - 1
    end
    return callbanker_times
end

function CMD.get_xiazhu_times(gamename, is_has_niu)
    local is_has_niu = is_has_niu or false
    local xiazhu_times = 1
    if is_has_niu == true then
        local total_w = slottool.calculate_total_weight(xiazhu_weights_niu)
        local xia_times = slottool.get_random_result_by_weightlist(xiazhu_weights_niu, total_w)
        xiazhu_times = xia_times
    else
        local total_w = slottool.calculate_total_weight(xiazhu_weights_noniu)
        local xia_times = slottool.get_random_result_by_weightlist(xiazhu_weights_noniu, total_w)
        xiazhu_times = xia_times
    end
    return xiazhu_times
end

function CMD.get_can_leveltable(game_times)
    local game_times = game_times or 1
    local is_leave = true
    if robot_leave_table_weight[game_times] then
        local base_temp = base.get_random(1,100)
        for index, value in ipairs(robot_leave_table_weight[game_times]) do
            base_temp = base_temp - value
            if base_temp <= 0 then
                if index == 1 then is_leave = false end
                break
            end
        end
    end
    if game_times == 0 then
        is_leave = false
    end
    return is_leave
end

function CMD.init()
    local tax_rate = 0.3   --默认税率0.3
    local matchtable_conf = tabletool.deepcopy(configdao.get_common_conf("matchtable_conf"))
    for key, matchitem in pairs(matchtable_conf) do
        local key = "dn_diamond_" .. tostring(key) .. "_rate"
        local result, result_data = redisdao.query_data(".gmmqredis", "get", key)
        if result and not result_data then
            redisdao.query_data(".gmmqredis", "set", key, tax_rate)
        end
    end
end


skynet.start(function()
	skynet.dispatch("lua",function(session, source, cmd, ...)
        local f = CMD[cmd]
        if f then
            skynet.retpack(f(...))
        else
            skynet.retpack()
        end
    end)
    CMD.init()
end)